/**
 * 获取用户信息列表
 * @param query
 */
function getUserData(query) {
    $('#userList')
        .DataTable(
            {
                "destroy": true,
                // "searching":false,
                "pagingType": "full_numbers",
                "sLoadingRecords": "正在加载数据...",
                "sZeroRecords": "暂无数据",
                "ajax": {
                    url: "/user/getUserList",
                    // dataSrc: 'data',//原来为空，修改为data 因为服务端模式返回的是data入参
                    dataSrc: "",
                    type:"post",
                    data : query,
                },
                // "serverSide": true,//服务端分页
                // "lengthChange": false,//是否允许用户自定义显示数量
                // "scrollY": "360px",//竖向滚动条
                // "scrollCollapse": "true",//是否出现滚动条
                // 'iDisplayLength':50,
                "searching": true,
                "columns": [
                    {
                        "data": "id"
                    },
                    {
                        "data": "name"
                    },
                    {
                        "data": "phone"
                    },
                    {
                        "data": "sex"
                    },
                    {
                        "data": "isVip"
                    },
                    {
                        "data": "vipStartTime"
                    },
                    {
                        "data": "vipEndTime"
                    },
                    {
                        "data": "isVipSend"
                    },
                    {
                        "data":"createTime"
                    },
                    {
                        "data": null,
                        "render": function (data, type, row, meta) {
                            var html =
                            //       "<a  href='#' role='button' class='btn btn-success btn-xs btn-green' data-toggle='modal' onclick='modifyUserDialog("
                            //     + JSON.stringify(row)
                            //     + ")'><i class='fa fa-pencil' ></i>修改</a>    "
                            //
                            //     + '<a href="#" role="button" class=\'btn btn-success btn-xs btn-remove\' data-toggle="modal"  onclick="showUserInfo('
                            //     + row.id
                            //     + ')"><i class="fa fa-trash-o"  ></i>查看</a>  '

                                 '<a href="#" role="button" data-toggle="modal" class=\'btn btn-warning btn-xs btn-remove\' onclick="giveUserVipDialog('
                                + JSON.stringify(row)
                                + ')"><i class="fa fa-trash-o"  ></i>赠送VIP</a>  '

                                + '<a href="#" role="button" data-toggle="modal" class=\'btn btn-warning btn-xs btn-remove\' onclick="giveUserCoupon('
                                + row.id
                                + ')"><i class="fa fa-trash-o"  ></i>赠送优惠券</a>  ';

                            return html;
                        }
                    }
                ],
                "language": {
                    "processing": "玩命加载中...",
                    "lengthMenu": "显示 _MENU_ 项结果",
                    "zeroRecords": "没有匹配结果",
                    "info": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                    "infoEmpty": "显示第 0 至 0 项结果，共 0 项",
                    "infoFiltered": "(由 _MAX_ 项结果过滤)",
                    "infoPostFix": "",
                    "url": "",
                    "paginate": {
                        "first": "首页",
                        "previous": "前一页",
                        "next": "下一页",
                        "last": "末页"
                    }
                }
            });
}

/**
 * 刷新
 */
function refreshUserData() {
    var b = {
        'id': $("#searchUserId").val() != "" ? $("#searchUserId").val() : 0,
        'name': $("#searchUserName").val(),
        'phone': $("#searchUserPhone").val(),
        'sex': $("#searchUserSex").val(),
        'isVip': $("#searchUserLevel").val(),
        'createTime':$('#searchUserCreateTime').val(),
        // "limit":1,
        // "page":1
    };
    $.ajax({
        // 请求方式
        type: "post",
        // 请求地址
        url: "/user/getUserList",
        data: JSON.stringify(b),
        contentType: 'application/json;charset=UTF-8',
        success: function (result) {
            b.recordsTotal=result;
            getUserData(b);
        },
        error: function (e) {
            console.log(e.status);
            console.log(e.responseText);
            alert("操作有误请联系管理员")
        }
    });
}

/**
 * 赠送用户VIP
 */
function giveUserVip(){
    var vipTime = $("select[name = sendUserVipTimeChoose]").val();
    var a = {
        "id": id,
        "vipTime": vipTime,
    };
    $.ajax({
        type:"PUT",
        url:"/user/giveUserVip",
        data: JSON.stringify(a),
        contentType: 'application/json;charset=UTF-8',
        success:function(result){
            $('#giveUserVip').modal('hide');
            console.log("赠送成功！");
            refreshUserData();
        },
        error:function(e){
            console.log(e.status);
            console.log(e.responseText);
            alert("操作有误请联系管理员！");
        }
    })
}

function giveUserVipDialog(reqVo){
    $('#giveUserVip').modal('show');
    id = reqVo.id;
    $("select[name = sendUserVipTimeChoose]").val(reqVo.vipTime);
}



/**
 * 赠送用户优惠券
 */
function giveUserCoupon(){
    $('#giveUserCoupon').modal('show');
}
$('#giveUserCouponButton').on('click',function(){
    var couponId = $("select[name = selectCouponId]").val();
    var number = $("input[name = giveUserCouponNumber]").val();
    var a = {
        "userId": id,
        "couponId": couponId,
        "number": number,
    };
    $.ajax({
        type:"PUT",
        url:"/user/giveUserCoupon",
        data:JSON.stringify(a),
        contentType: 'application/json;charset=UTF-8',
        success:function(result){
            $('#giveUserVip').modal('hide');
            alert("赠送成功！");
            refreshUserData();
        },
        error:function(e){
            console.log(e.status);
            console.log(e.responseText);
            alert("操作有误请联系管理员！")
        }
    })
})

function giveUserCouponDialog(vo){
    $('#giveUserCoupon').modal('show');
    id = vo.userId;
    $("select[name = selectCouponId]").val(vo.couponId);
    $("input[name = giveUserCouponNumber]").val(vo.number);
}
